package com.zy.servlet.car;

import com.zy.service.ClearCarService;
import com.zy.service.impl.ClearCarServiceImpl;
import jakarta.servlet.ServletException;

import jakarta.servlet.annotation.WebServlet;

import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;



@WebServlet("/clearCar")
public class ClearCarServlet extends HttpServlet {
    private ClearCarService clearCarService = new ClearCarServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("ClearCarServlet");
        doPost(request, response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("ClearCarServlet");
        int userId = Integer.parseInt(request.getParameter("userid"));
        boolean success = clearCarService.clearCar(userId);
        if (success) {
            response.setContentType("application/json");
            response.getWriter().write("{\"success\": true}");
        } else {
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Failed to clear cart");
        }
        System.out.println("ClearCarServlet");
    }
}